package java_1124;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-11-24
 * Time:18:29
 */
public class TsetJDBCSelect {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java_1124?characterEncoding=utf-8");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("xcb@19980521");

        Connection connection =dataSource.getConnection();

        String sql = "select * from student";

        PreparedStatement statement = connection.prepareStatement(sql);
        //循环遍历
        ResultSet resultSet = statement.executeQuery();
        while (resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int class_id = resultSet.getInt("class_id");
            System.out.println("id: " +id + " name: " + name + " class_id: " + class_id );
        }
        resultSet.close();
        statement.close();
        connection.close();
    }

}
